package org.frog.sgs.server.game;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.frog.sgs.card.role.Role;
import org.frog.sgs.card.scheme.base.WuXieKeJi;
import org.frog.sgs.character.Character;
import org.frog.sgs.data.GameDataManager;
import org.frog.sgs.server.ServerConfig;
import org.frog.sgs.server.game.status.ServerStatus;
import org.frog.sgs.server.msg.ServerMsgChooseRole;
import org.frog.sgs.server.msg.ServerMsgZhuGong;

/**
 * @author duanxiangqi
 * @date 2012-10-20
 */
public class ServerInstance implements Serializable {

	private static final long serialVersionUID = -4675193765006989452L;

	private ServerConfig config;

	private ServerStatus status;

	private ServerCharacterManager characterManager;

	private ServerCardManager cardManager;

	private ServerEventManager eventManager;

	public ServerInstance() {}

	public ServerInstance(ServerConfig config) {
		this.config = config;
	}

	public void init() {
		status = ServerStatus.waitingCharacter;
		characterManager = new ServerCharacterManager(this);
		cardManager = new ServerCardManager(this);
		cardManager.init();
		eventManager = new ServerEventManager();
	}

	public void start() {
		// 打乱玩家位置
		if (status != ServerStatus.waitingCharacter) {
			return;
		}
		status = ServerStatus.allocateRole;
		// 分配角色
		characterManager.shuffle();
		int[] roles = config.getRoles();
		List<Role> allRolesCopy = GameDataManager.instance.getAllRolesCopy();
		Collections.shuffle(allRolesCopy);
		int position = 0;
		for (Role role : allRolesCopy) {
			if (Arrays.binarySearch(roles, role.getId()) >= 0) {
				Character character = characterManager.getByPosition(position++);
				character.setRole(role);
				role.setCharacter(character);
				character.sendMsg(new ServerMsgChooseRole(character));
				System.out.println("分配角色" + character.getViewName());
			}
		}
		// 分配武将
		status = ServerStatus.allocateGeneral;
		Character zhugong = characterManager.getZhuGong();
		zhugong.sendMsgToOther(new ServerMsgZhuGong(zhugong));
		zhugong.sendChooseGenerals();
	}

	/**
	 * 判断是否有玩家手牌中有无懈可击
	 * 
	 * @return
	 */
	public boolean hasWuXieKeJiInHands() {
		for (Character character : characterManager.getCharacters()) {
			if (character.hasCardInHand(WuXieKeJi.class)) {
				return true;
			}
		}
		return false;
	}

	public boolean isBeforePlaying() {
		return status.getValue() <= ServerStatus.waitingCharacter.getValue();
	}

	public boolean isPlaying() {
		return status.getValue() > ServerStatus.waitingCharacter.getValue()
				&& status.getValue() <= ServerStatus.playing.getValue();
	}

	public boolean isOver() {
		return status == ServerStatus.over;
	}

	public ServerCharacterManager getCharacterManager() {
		return characterManager;
	}

	public void setCharacterManager(ServerCharacterManager characterManager) {
		this.characterManager = characterManager;
	}

	public ServerCardManager getCardManager() {
		return cardManager;
	}

	public void setCardManager(ServerCardManager cardManager) {
		this.cardManager = cardManager;
	}

	public ServerConfig getConfig() {
		return config;
	}

	public void setConfig(ServerConfig config) {
		this.config = config;
	}

	public ServerStatus getStatus() {
		return status;
	}

	public void setStatus(ServerStatus status) {
		this.status = status;
	}

	public ServerEventManager getEventManager() {
		return eventManager;
	}

	public void setEventManager(ServerEventManager eventManager) {
		this.eventManager = eventManager;
	}
}
